package gsw.demo.permission;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import gsw.tool.permission.annotation.Permission;

/**
 * 动态权限申请Demo
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_all).setOnClickListener(this);
        findViewById(R.id.btn_all_exclude).setOnClickListener(this);
        findViewById(R.id.btn_one_permission).setOnClickListener(this);
        findViewById(R.id.btn_two_permission).setOnClickListener(this);
        findViewById(R.id.btn_request_200).setOnClickListener(this);
        findViewById(R.id.btn_service).setOnClickListener(this);

        //设置各自品牌的系统权限页
        //JPermissionUtil.setManuFacturer("genymotion", MyTestGenymotionMenu.class);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.frame_layout, new MyFragment());
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_all) {
            requestAll();
        } else if (view.getId() == R.id.btn_all_exclude) {
            requestAllExclue();
        } else if (view.getId() == R.id.btn_one_permission) {
            requestOnePermission();
        } else if (view.getId() == R.id.btn_two_permission) {
            requestTwoPermission();
        } else if (view.getId() == R.id.btn_request_200) {
            requestRequest200();
        } else if (view.getId() == R.id.btn_service) {
            requestService();
        }
    }

    /**
     * 申请manifest中的权限
     */
    private void requestAll() {
    }

    /**
     * 申请manifest中的权限（剔除相机权限）
     */
    private void requestAllExclue() {
    }

    /**
     * 请求写权限(一个权限)
     */
    @Permission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    private void requestOnePermission() {
        Toast.makeText(this, "请求一个权限成功（写权限）", Toast.LENGTH_SHORT).show();
    }

    /**
     * 请求相机和电话(两个权限)
     */
    @Permission({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA})
    private void requestTwoPermission() {
        Toast.makeText(this, "请求两个权限成功（写和相机）", Toast.LENGTH_SHORT).show();
    }

    /**
     * 请求定位(回调值200)
     */
    @Permission(value = {Manifest.permission.ACCESS_FINE_LOCATION}, requestCode = 200)
    private void requestRequest200() {
        Toast.makeText(this, "请求定位权限成功，200", Toast.LENGTH_SHORT).show();
    }

    /**
     * 打开服务
     */
    private void requestService() {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
}
